﻿Public Class ArticleAmountModel
    Implements IComparable

    Private _amount As Double
    Private _article As Article
    Private _proceeds As Money

    Public Sub New()
        _amount = 0
        _proceeds = New Money(0)
    End Sub

    Public Sub New(article As Article)
        Amount = 0
        _proceeds = New Money(0)
        _article = article
    End Sub

    Public Sub New(article As Article, amount As Double)
        _article = article
        _amount = amount
    End Sub

    Public Sub New(article As Article, amount As Double, proceeds As Money)
        _article = article
        _amount = amount
        _proceeds = proceeds
    End Sub

    ''' <summary>
    ''' How many units were saled
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Amount As Double
        Get
            Return _amount
        End Get
        Set(value As Double)
            _amount = value
        End Set
    End Property

    Public Property Article As Article
        Get
            Return _article
        End Get
        Set(value As Article)
            _article = value
        End Set
    End Property

    ''' <summary>
    ''' How much money you gained from it
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Proceeds As Money
        Get
            Return _proceeds
        End Get
        Set(value As Money)
            _proceeds = value
        End Set
    End Property

    Public ReadOnly Property AmountString As String
        Get
            If _article IsNot Nothing Then
                If _article.ArticleUnit IsNot Nothing Then
                    Return Amount.ToString("0.000").Replace(".", ",") + " " + _article.ArticleUnit.ShortName
                End If
                Return Amount.ToString("0.000").Replace(".", ",")
            End If
            Return ""
        End Get
    End Property

    Public Function CompareTo(obj As Object) As Integer Implements System.IComparable.CompareTo
        Try
            Dim item As ArticleAmountModel = obj
            Return Amount - item.Amount
        Catch ex As Exception : End Try
        Return 0
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        Try
            Dim item As ArticleAmountModel = obj
            Return item.Article.ArticleID = Article.ArticleID
        Catch ex As Exception : End Try
        Return False
    End Function
End Class

Public Class ArticleAmountProceedsSort
    Implements IComparer(Of ArticleAmountModel)

    Public Function Compare(x As ArticleAmountModel, y As ArticleAmountModel) As Integer Implements System.Collections.Generic.IComparer(Of ArticleAmountModel).Compare
        Return (y.Proceeds - x.Proceeds).Amount
    End Function
End Class
